package jspservlet.dao.impl;

import java.io.Console;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.http.HttpSession;

import com.google.protobuf.TextFormat.Parser;
import com.mysql.cj.xdevapi.JsonArray;

import jspservlet.dao.UserDAO;
import jspservlet.vo.User;
import jspservlet.vo.Connection;
import org.json.JSONArray.*;
import org.json.JSONObject.*;
import org.json.*;


public class UserDAOImpl implements UserDAO {

//1
	public int login(User user) throws Exception {
	
		Connection conn=new Connection();

		JSONObject TEST=new JSONObject();

		TEST.put("route","webLogin");
		JSONObject test=new JSONObject();
		test.put("action",1);
		test.put("username",user.getUsername());
		test.put("password",user.getPassword());
		TEST.put("data",test);
		JSONObject returnMsg=conn.transport(TEST);

		int success=returnMsg.getInt("success");
		if (success==1) 
			System.out.println("登陆成功"); 
		else 
			System.out.println("登陆失败");
		return success;
	}    

//2
	public int registeUser(User user) throws Exception{
		
		Connection conn=new Connection();

		JSONObject TEST=new JSONObject();

		TEST.put("route","webLogin");
		JSONObject test=new JSONObject();
		test.put("action",2);
		test.put("username",user.getUsername());
		test.put("password",user.getPassword());
		TEST.put("data",test);

		JSONObject returnMsg=conn.transport(TEST);

		int success=returnMsg.getInt("success");
		if (success==1) 
			System.out.println("登陆成功"); 
		else 
			System.out.println("登陆失败");
		return success;
	}

//5	
	public int changePsw(User user , HttpSession session) throws Exception{
		Connection conn=new Connection();

		JSONObject TEST=new JSONObject();

		TEST.put("route","changepsw");
		JSONObject test=new JSONObject();
		test.put("username",user.getUsername());
		test.put("oldpassword",user.getPassword());
		test.put("newpassword",user.getDob());
		TEST.put("data",test);

		JSONObject returnMsg=conn.transport(TEST);

		int success=returnMsg.getInt("success");
		return success;
	}
//6
	public int modifyinfo(User user, HttpSession session) throws Exception{
		Connection conn=new Connection();

		JSONObject TEST=new JSONObject();

		TEST.put("route","updateInfo");
		JSONObject test=new JSONObject();
		test.put("username",user.getUsername());
		test.put("gender",user.getGender());
		test.put("dateOfBirth",user.getDob());
		test.put("familyID",user.getFamilyid());
		test.put("phoneNum",user.getPassword());
		TEST.put("data",test);

		JSONObject returnMsg=conn.transport(TEST);

		int success=returnMsg.getInt("success");
		return success;
	}
	//7	
	public int showUserInfo(User user, HttpSession session) throws Exception{
		//展示个人信息
		
		//String msg= "{\"route\":\"showuserinfo\",\"data\":{\"username\":"+session.getAttribute("username").toString()+"}}";
		//test client=new test();
		//String response = client.sendMsg(msg);

		//JSONObject json = new JSONObject(response);
		//JsonArray data = json.getJSONArray(key: "uploads");
		//int mode = 0;
		//JSONArray orders = new JSONArray();
		//orders.put();

		Connection conn=new Connection();

		JSONObject TEST=new JSONObject();

		TEST.put("route","showuserinfo");
		JSONObject test=new JSONObject();
		test.put("username",session.getAttribute("username").toString());
		TEST.put("data",test);
		JSONObject returnMsg=conn.transport(TEST);


		int success=returnMsg.getInt("success");
		JSONObject returnData=returnMsg.getJSONObject("data");
		String gender=returnData.getString("gender");
		String DoB = returnData.getString("dateOfBirth");
		String phonenumber = returnData.getString("phoneNum");
		String familyID = returnData.getString("familyID");
		return success;
	}
	public int showDeviceInfo(User user) throws Exception{
		Connection conn=new Connection();

		JSONObject TEST=new JSONObject();
		TEST.put("route","showuserinfo");
		JSONObject test=new JSONObject();
		test.put("username",user.getUsername());
		TEST.put("data",test);
		JSONObject returnMsg=conn.transport(TEST);

		JSONObject returnData=returnMsg.getJSONObject("data");
		//JSONArray deviceDatas = returnMsg.parseArray(Parser.getString("data").toString);
		//JSONArray deviceDatas=new JSONArray();
		JSONArray deviceDatas= returnData.getJSONArray("data");

			for(Object deviceData:deviceDatas){
				JSONObject deviceMsg=(JSONObject)deviceData;
				switch( returnData.getString("Type") ){
					case "Fan":

					"deviceID": "String",
					"State": "int",
					"Temp": "Float",
					"OpenTemp": "Float",
					"Humi": "Float"
				
					case "Door":
					"deviceID": "String",
					"State": "int",
					"Password": "int",
					"OpenTimes": "int"

					case "Window":
					"deviceID": "String",
					"State": "int",
					"OpenTimes": "int"

					case "Lamp":
					"deviceID": "String",
					"State": "int",
					"Color": "int",
					"R": "int",
					"G": "int",
					"B": "int",
					"MaxLuminance": "int"

				   case "Alarm":
					"deviceID": "String",
					"AlarmTriggered": "int",
					"AlarmMode": "int"

				  case "Cover":
					"deviceID": "String",
					"Illumination": "Float",
					"OpenIllumination": "Float"

				 case "FireAlarm":
					"deviceID": "String",
					"AlarmTriggered": "int"
				   
				}
			}
		int success=returnMsg.getInt("success");
		return success;
	}

	public int showCondition(String username) throws Exception{
		
		Connection conn=new Connection();

		JSONObject TEST=new JSONObject();

		TEST.put("route","showsecuritiyinfo");
		JSONObject test=new JSONObject();
		test.put("username",username);
		TEST.put("data",test);
		JSONObject returnMsg=conn.transport(TEST);

		int success=returnMsg.getInt("success");
		JSONObject returnData=returnMsg.getJSONObject("data");
		int BreakInAlarm=returnData.getInt("BreakInAlarm");
		int FireAlarm = returnData.getInt("FireAlarm");
		String Temp = returnData.getString("Temp");
		String Humi = returnData.getString("Humi");
		return success;

	}
}


